package com.million.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.million.staservice.client.UcenterClient;
import com.million.staservice.entity.StatisticsDaily;
import com.million.staservice.mapper.StatisticsDailyMapper;
import com.million.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.staservice.strategy.TypeStrategyFactory;
import org.apache.commons.collections4.queue.UnmodifiableQueue;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-04-11
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private TypeStrategyFactory typeStrategyFactory;


    //服务调用查询到的注册人数将注入人数写入到statistics_daily表中
    @Override
    public void registerCount (String day) {

        //为了保证在同一天注册的人数可能会有 变化，可以先把当天之前的数据删除，然后再添加最新的数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);
        //服务调用
        Integer registerUcenter = ucenterClient.getRegisterUcenter(day);

        //把获取添加数据库，统计分析表里面
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(registerUcenter);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(statisticsDaily);
    }

    //将生成的数据通过图标的方式显示出来 返回json 日期数组  json 数量数组
    @Override
    public Map<String, Object> getShowData (String type, String beginDate, String endDate) {
        //查询
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated", beginDate, endDate);
        //这里的type 字段需要和数据库中的一致，也就是前端的字段要和数据库一致
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(queryWrapper);
        //因为返回有两部分，日期和日期对应的数量
        //前端要求数据是json结构，对应后端的是list集合
        //创建两个集合，一个日期 一个数量
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> registerNumList = new ArrayList<>();
        ArrayList<Integer> loginNumList = new ArrayList<>();
        ArrayList<Integer> videoViewNumList = new ArrayList<>();
        ArrayList<Integer> courseNumList = new ArrayList<>();
        for (StatisticsDaily daily : statisticsDailies) {
            //添加日期
            dateList.add(daily.getDateCalculated());
            //添加对应查询的条件 《使用策略模式 但是不知道是调用了哪个实现类》
            registerNumList.add(daily.getRegisterNum());
            loginNumList.add(daily.getLoginNum());
            videoViewNumList.add(daily.getVideoViewNum());
            courseNumList.add(daily.getCourseNum());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculated", dateList);
        map.put("registerNumList", registerNumList);
        map.put("loginNumList", loginNumList);
        map.put("videoViewNumList", videoViewNumList);
        map.put("courseNumList", courseNumList);
        return map;
    }
}
